<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="loginBtn">点击创建悬浮窗</button>
    <script>
      // 需要的时候才创建对象的实例

      var createLoginLayer = (function () {
        var div
        return function () {
          if (!div) {
            div = document.createElement('div')
            div.innerHTML = '我是登录悬浮窗'
            div.style.display = 'none'
            document.body.appendChild(div)
          }

          return div
        }
      })()
      document.getElementById('loginBtn').onclick = function () {
        var loginLayer = createLoginLayer()
        loginLayer.style.display = 'block'
      }
    </script>
  </body>
</html>
